﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ERF;
using ERF.Menu;


namespace FirstPerson.States
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class EndState : State
    {
        /// <summary>
        /// Singleton instance
        /// </summary>
        private static EndState instance;
        private ScoreBoard scoreBoard;

        /// <summary>
        /// Get an instance of this state.
        /// </summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        public static EndState GetInstance(StateMachine machine)
        {
            if (instance == null)
                instance = new EndState(machine);

            return instance;
        }

        /// <summary>
        /// Constructor of EndState
        /// </summary>
        /// /// <summary>
        /// <param name="machine">the state machine that this state should belong to.</param>
        /// <returns></returns>
        private EndState(StateMachine machine)
            : base(machine)
        {
            this.scoreBoard = (ScoreBoard)machine.CurrentGame.Services.GetService(typeof(IScoreBoard));           
        }

        public override void Initialize()
        {
            Menu menu = new Menu("BattleSquadron project");
            menu.MenuItems.Add(new MenuItem("Play again", this.GotoStateStartMenu));
            menu.MenuItems.Add(new MenuItem("Quit", this.QuitGame));
            menu.SelectedIndex = 0;
        }

        private void GotoStateStartMenu()
        {
            PlayingState playingState = PlayingState.GetInstance(base.machine);
            playingState.EndMatch();

            base.machine.CurrentState = StartMenuState.GetInstance(base.machine);
        }

        private void QuitGame()
        {
            this.machine.CurrentGame.Exit();
        }


        /// <summary>
        /// Begin PlayingState and hosting the game
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (input.IsButtonReleased("MENU_BUTTON"))
            {
                this.GotoStateStartMenu();
            }
        }
    }
}